import { createApp } from 'vue'
import App from './App.vue'


// 引入路由
import Router from '@/routers/index'

// 引入vantUI框架
import * as Vant from 'vant'
import 'vant/lib/index.css';

// 引入Cookie
import  VueCookies from 'vue3-cookies';

// 引入接口
import Api from '@/api/index'

const app = createApp(App)
.use(Router)
.use(Vant)
.use(VueCookies)
.use(Api)
.mount('#app')


// 前置路由首位
Router.beforeEach(async(to,from,next)=> {
    /* 
        to => 目标地址
        from => 来源地址
        next => 放开

        to => business/base/index
        from  => home
    */
    //  获取目标地址的登录认证是否存在
    let IsLogin = to.meta.auth === true ? true : false

    if(IsLogin === true)
    {
      // 通过cookie去获取登录信息
      let LoginAuth = app.$cookies.get('LoginAuth') ? app.$cookies.get('LoginAuth') : {}

      if(JSON.stringify(LoginAuth)==='{}')
      {
        Vant.showToast({
          type:'fail',
          message:'请先登录',
          duration:1500,
         
        })
        next('/business/base/login')

      }

      let mobile = LoginAuth.mobile ? LoginAuth.mobile : ''
      let busid = LoginAuth.id ? LoginAuth.id : 0

      let result = await app.$api.check({mobile,busid})

      if(result.code === 0)
      {
          Vant.showNotify({
              type:'warning',
              message:result.msg,
              duration:1500,
              
          })

          app.$cookies.remove('LoginAuth')
          next('/business/base/login')


      }

      app.$cookies.set('LoginAuth',result.data)

      next()

    }else{  
      next()
    }

}) 